package work.xiaohong.arithmetic.IO;

import lombok.Cleanup;

import java.io.*;
import java.util.Map;
import java.util.Properties;

/**
 * @author xiaohong
 * @version 1.0
 * @date 2021/10/27 0027 20:09
 * @description
 * Properties:属性集合。特点
 * 1.存储属性名和属性值
 * 2.属性名和属性值都是字符串类型.
 * 3.没有泛型
 * 4.和流有关
 */
public class PropertiesTest {

    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.setProperty("name","小鸿");
        properties.setProperty("age","18");

//        System.out.println(properties.toString()); //{name=xiaohong, age=18}

        // 遍历
        /**
         * name=====xiaohong
         * age=====18
         */
//        for (Object o : properties.keySet()) {
//            System.out.println(o + "=====" + properties.get(o));
//        }

        /**
         * name=xiaohong
         * age=18
         */
//        for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) {
//            System.out.println(objectObjectEntry);
//        }


        /**
         * name=====xiaohong
         * age=====18
         */
//        for (String propertyName : properties.stringPropertyNames()) {
//            System.out.println(propertyName + "=====" + properties.get(propertyName));
//        }

        // 与流相关
//        @Cleanup
//        PrintWriter printWriter = new PrintWriter("c:\\print.txt");
//        properties.list(printWriter);

        // 持久化
//        @Cleanup
//        FileOutputStream fileOutputStream = new FileOutputStream("c:\\print.properties");

          // 有中文用这个
//        @Cleanup
//        FileWriter fileWriter = new FileWriter("c:\\print.properties");
//        properties.store(fileWriter,"注释");


        // 加载
        Properties properties1 = new Properties();
        // 有中文用这个
        @Cleanup
        FileReader fileReader = new FileReader("c:\\\\print.properties");
//        @Cleanup
//        FileInputStream fileInputStream = new FileInputStream("c:\\print.properties");
        properties1.load(fileReader);
        System.out.println(properties1.toString());

    }

}
